package com.cn.learn.jpa.domain.jpa;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

/**
 * @ClassName Game
 * @Description 游戏实体类
 * @Author JiJiang
 * @Date 2022/9/22 16:56
 * @Version 1.0
 */
@Table(name = "game") //设置表的名字
@Entity //标记为实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Game {


    @Id //设置该属性为id 即数据库中的key
    @GeneratedValue(strategy = GenerationType.IDENTITY) //设置id的生成策略为自动生成
    @Column(name = "game_id")
    private Long gameId;

    @Column(name = "game_name")
    private String gameName;


    @Column(name = "create_time")
    private Date createTime;

    @Column(name = "update_time")
    private Date updateTime;

    //指定多对一关系中，一的类型，
    //这里从游戏角度看的话，一个游戏只能对一个游戏类型，而一个游戏类型可以对应多个游戏
    @ManyToOne(targetEntity = GameType.class,cascade = CascadeType.PERSIST)
    //name是从表的外键，referencedColumnName为主表的关联的字段名
    @JoinColumn(name = "gt_id", referencedColumnName = "type_id")
    private GameType gameType;


    @ManyToMany
    @JoinTable(name = "game_feature_rel",//中间表的名称
            // 中间表game_feature_rel game_id字段 关联 sys_role表的主键字段role_id
            joinColumns = {@JoinColumn(name = "g_id", referencedColumnName = "game_id")},
            // 中间表game_feature_rel feature_id字段 关联 sys_user表的主键user_id
            inverseJoinColumns = {@JoinColumn(name = "f_id", referencedColumnName = "feature_id")}
    )
    private Set<GameFeature> gameFeatures;


    @Override
    public String toString() {
        return "Game{" +
                "id=" + gameId +
                ", gameName='" + gameName + '\'' +
                '}';
    }
}
